import tkinter as tk
from ...shoudongfasong.ui_demo_toplevel import ZCAN_Demo_Toplevel
from ....utils.loggers import ic_debugger
from tkinter import messagebox
import time
import uuid
from ....zlgcan.zlgcan import *
from ....utils.msg_queue import global_can_unitTestRecvQueues
from ....utils.types import CanMsg_std_type


MAX_RCV_NUM = 10


class Can_ManualTab_Frame(tk.Frame):
    def __init__(self, master, masterUI, parentWin):
        super().__init__(master=master)
        self.masterUI = masterUI
        self.parentWin = parentWin

        self.can_manualTabInit()

###############################################################################
# UI绘制
###############################################################################

    def can_manualTabInit(self):
        self.can_manualTab_btn = tk.Button(
            self, text='打开手动can界面', command=self.can_manualTab_toplevel_init)
        self.can_manualTab_btn.pack()

        self.tempBtn = tk.Button(
            self, text='打开readMsg', command=self.test_readMsg_open)
        self.tempBtn.pack()

        self.tempBtn = tk.Button(
            self, text='关闭readMsg', command=self.test_readMsg_close)
        self.tempBtn.pack()

        # 测试queue
        self.test_queue_creat()

    def test_queue_creat(self):
        msg_queue_name = "test_q1"
        test_q1_key_symbol = '202405121201'

        filter_ids = [0x506, 0x507]
        self.test_q1 = self.masterUI.recv_queues.create_queue(
            key_symbol=test_q1_key_symbol, name=msg_queue_name, filter_ids=filter_ids)

    def test_readMsg_open(self):
        self.masterUI.readMsg_event.set()

    def test_readMsg_close(self):
        # print(self.test_q1)
        # FIXME： 此处不能直接迭代deque，否则会出现线程资源冲突，要先转化为list，然后再处理
        # for msg in list(self.test_q1.deque):
        # print(msg.can_id)
        self.masterUI.readMsg_event.clear()

    def can_manualTab_toplevel_init(self):
        self.can_manualTab_toplevel = ZCAN_Demo_Toplevel(
            masterUI=self.masterUI, parentWin=self)
        pass


###############################################################################
# 自定义Backend增加函数
###############################################################################
